<?php
namespace app\api\model;
use plugin\filter;
use think\Db;
use think\Model;
use app\api;

/*用户信息模型*/
class UserInfo extends CommonModel
{
    private $table_name = 'user_info';
    public function __construct($args = [])
    {
        parent::__construct($args);
    }

    public function defaultParam($args)
    {
        $data = [
            'client_id' => '',
            'name'      => "匿名",
            'company'   => "",
            'phone'     => "",
            'remark'    => "",
            'add_time'  => time()
        ];
        return array_merge($data,$args);
    }

    //新增
    public function add($args)
    {
        $data = $this->defaultParam($args);
        if(empty($data['client_id']))
        {
            return ['error'=> 1 ,'msg'=> '没有指定设置的用户'];
        }

        //更新user表
        $userModel = new Users();
        $userModel->updateNickName(['client_id'=>$data['client_id'],'nickname'=>$args['name']]);

        db($this->table_name)->insertGetId($data);

        return ['error'=>0,'msg'=>"设置完毕"];
    }

    //修改
    public function edit($args)
    {
        $data = $this->defaultParam($args);
        unset($data['add_time']);
        if(empty($data['id']) && empty($data['client_id']))
        {
            return ['error'=>1 , 'msg'=> "没有指定用户进行修改"];
        }

        $where = [];
        if(!empty($data['id']))
        {
            $where['id'] = $data['id'];
        }

        if(!empty($data['client_id']))
        {
            $where['client_id'] = $data['client_id'];

            //更新user表
            $userModel = new Users();
            $userModel->updateNickName(['client_id'=>$data['client_id'],'nickname'=>$args['name']]);
        }

        unset($data['client_id'],$data['id']);

        //更新user_info表
        db($this->table_name)->where($where)->update($data);

        return ['error'=>0,'msg'=>"更新完毕"];
    }

    //查询一个
    public function info($args)
    {
        /**
         * args参数说明
         * args.client_id args.id可以根据这两个进行查询
         */
        if((!isset($args['id']) || empty($args['id']))
            &&
            (!isset($args['client_id']) || empty($args['client_id']))
        )
        {
            return ['error'=>1 ,'msg'=> "没有查询条件"];
        }

        $where = [];
        if(!empty($args['id']))
        {
            $where['id'] = $args['id'];
        }

        if(!empty($args['client_id']))
        {
            $userModel = new Users();
            $userInfo = $userModel->user_info(['client_id'=>$args['client_id']]);
            if(empty($userInfo))
            {
                return ['error'=>1,"msg"=>"该用户不存在"];
            }
            $where['client_id'] = $userInfo['client_id'];
        }


        $data = db($this->table_name)->where($where)->find();
        if(!empty($data))
        {
            $data['add_time'] = date('Y-m-d H:i',$data['add_time']);
        }
        unset($data['client_id'],$data['id']);
        return ['error'=>0 ,'msg'=> $data];
    }
}